package com.chunxiadong.controller.admin;

import com.chunxiadong.entity.Blog;
import com.chunxiadong.entity.BlogBySearch;
import com.chunxiadong.entity.Type;
import com.chunxiadong.entity.User;
import com.chunxiadong.service.BlogService;
import com.chunxiadong.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @auther QiuShangcheng
 * @create 2021/6/7
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogController {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    TypeService typeService;

    @Autowired
    BlogService blogService;

    //跳转博客发布页面
    @RequestMapping("/input")
    public String inputBlog(Model model, HttpSession session) {
        session.setAttribute("types", typeService.getAllType());
        model.addAttribute("blog", new Blog());
        return "admin/blog-input";
    }


    //    博客新增
    @RequestMapping("/addBlog")
    public String addBlog(Blog blog, RedirectAttributes model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        blog.setUserId(user.getId());
        int i = blogService.addBlog(blog);
        if (i == 0) {
            model.addFlashAttribute("blog_message", " Add_Blog Case Error!");
            log.info(blog.toString() + "\nResult : {" + " Add_Blog Case Error!" + "}");
        } else {
            model.addFlashAttribute("blog_message", "Add_Blog Successful!");
        }
        return "redirect:/admin/blog";
    }

    //    编辑修改文章
    @PostMapping("/updateBlog/{id}")
    public String updateBlog(Blog blog, RedirectAttributes model, @PathVariable Integer id) {
        Blog queryRes = blogService.getBlog(id);
        if (queryRes == null){
            model.addAttribute("blog_message", "No Blog_Data!");
            log.info(blog.toString() + "\nResult : {" + " No Blog_Data!" + "}");
            return "redirect:/admin/blog";
        }
        int i = blogService.updateBlog(blog);
        if (i == 0) {
            model.addFlashAttribute("blog_message", " Update_Blog Case Error!");
            log.info(blog.toString() + "\nResult : {" + " Update_Blog Case Error!" + "}");
        } else {
            model.addFlashAttribute("blog_message", "Update_Blog Successful!");
        }
        return "redirect:/admin/blog";
    }

    //    删除文章
    @GetMapping("/{id}/delete")
    public String deleteBlog(@PathVariable Integer id, RedirectAttributes model) {
        int i = blogService.deleteBlogById(id);
        if (i == 0) {
            model.addFlashAttribute("blog_message", " Delete_Blog Case Error!");
            log.info("Result : {" + "Delete_Blog Case Error!" + "}");
        } else {
            model.addFlashAttribute("blog_message", "Delete_Blog Successful!");
        }
        return "redirect:/admin/blog";
    }

    //    跳转编辑修改文章
    @RequestMapping("/{id}/input")
    public String inputUpdateBlog(@PathVariable Integer id, Model model,HttpSession session) {
        Blog blog = blogService.getBlogToUpdate(id);
        log.info(blog.toString()+"---------------------------------");
        List<Type> allType = typeService.getAllType();
        model.addAttribute("blog", blog);
        session.setAttribute("types", allType);
        return "admin/blog-input";
    }

       //  搜索
    @RequestMapping("/search")
    public String searchBlog(Integer typeId, String title, Model model
    , @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        BlogBySearch blogBySearch = null;
        if (typeId != null && title.isEmpty() == true){
             blogBySearch = new BlogBySearch(typeId, null,null);
        }else if (typeId == null && title.isEmpty() == false){
             blogBySearch = new BlogBySearch(null, "%"+title+"%",null);
        }else  blogBySearch = new BlogBySearch(typeId, "%"+title+"%",null);

        PageInfo<Blog> pageInfo = blogService.getSearchBlogByPage(pageNum, 100, blogBySearch);
        model.addAttribute("pageInfo", pageInfo);

        return "admin/search";
    }

}
